﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wicresoft.UnifyShow.Business;
using Microsoft.Practices.Unity;

namespace Wicresoft.UnifyShow.Portal.Controllers
{
    public class BasicEnvironmentAllController : BaseController
    {
        private IIncident incident;
        private IChange change;
        private IProblem problem;
        private ISiteBranch siteBranch;
        private IUser_X user;
        private ISLA sla;

        protected override void Dispose(bool disposing)
        {
            ((IDisposable)incident).Dispose();
            ((IDisposable)change).Dispose();
            ((IDisposable)problem).Dispose();
            ((IDisposable)siteBranch).Dispose();
            ((IDisposable)user).Dispose();
            ((IDisposable)sla).Dispose();
        }

        [InjectionConstructor]
        public BasicEnvironmentAllController(IIncident iincident, IChange ichange, IProblem iproblem,
            ISiteBranch isiteBranch, IUser_X iuser, ISLA sla)
        {
            incident = iincident;
            change = ichange;
            problem = iproblem;
            siteBranch = isiteBranch;
            user = iuser;
            this.sla = sla;
        }

        //
        // GET: /BasicEnvironmentAll/

        public ActionResult Index()
        {
            base.SetRoles(user);
            if (UserID == -1) return new HttpStatusCodeResult(403);
            sla.UserID = UserID;

            IList<SiteBranchModel> sites = siteBranch.GetSiteALLBranch();
            IList<DeviceAndAlertCountModel> alertCount = new List<DeviceAndAlertCountModel>();
            IList<SiteBranchAndWorkListModel> incidentList = new List<SiteBranchAndWorkListModel>();
            IList<SiteBranchAndWorkListModel> changeList = new List<SiteBranchAndWorkListModel>();
            IList<SiteBranchAndWorkListModel> problemList = new List<SiteBranchAndWorkListModel>();

            foreach (var item in UserSiteIDs)
            {
                int siteID = int.Parse(item.ToString());
                alertCount = alertCount.Concat(siteBranch.GetSiteAlertCountBySiteID(siteID)).ToList();
                incidentList = incidentList.Concat(incident.GetIncidentInfoList(siteID, null, null, null)).ToList();
                changeList = changeList.Concat(change.GetChangeInfoList(siteID, null, null, null)).ToList();
                problemList = problemList.Concat(problem.GetProblemInfoList(siteID, null, null, null)).ToList();
            }
            List<SiteAndSLASumModel> slaList = sla.GetSiteAndSLA();

            ViewData["Sites"] = sites;
            ViewData["SitesMe"] = UsersSites;
            ViewData["AlertCount"] = alertCount;
            ViewData["Incidents"] = incidentList;
            ViewData["Changes"] = changeList;
            ViewData["Problems"] = problemList;
            ViewData["SLAS"] = slaList;
            return View();
        }

    }
}
